package com.sims.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sims.bean.Manager;
import com.sims.bean.Student;
import com.sims.bean.Teacher;
import com.sims.dao.IManagerDAO;
import com.sims.dao.IStudentDAO;
import com.sims.dao.ITeacherDAO;
import com.sims.util.Constant;
import com.sims.util.HttpSessionUtil;

public class ManagerService {
	@Autowired
	IManagerDAO managerDAO;
	@Autowired
	IStudentDAO studentDAO;
	@Autowired
	ITeacherDAO teacherDAO;

	public Map<String, String> addStudent(Student student) {
		Map<String, String> result = new HashMap<String, String>();

		if (studentDAO.findBySno(student.getSno()) != null) {
			result.put(Constant.ERROR, "Student Already Exists");
			return result;
		}

		System.out.println(student.getSno() + student.getPassword());

		studentDAO.attachDirty(student);

		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> addTeacher(Teacher teacher) {
		Map<String, String> result = new HashMap<String, String>();

		if (teacherDAO.findByTno(teacher.getTno()) != null) {
			result.put(Constant.ERROR, "Teacher Already Exists");
			return result;
		}

		teacherDAO.attachDirty(teacher);

		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> findStudent(List<Student> student) {
		Map<String, String> result = new HashMap<String, String>();
		@SuppressWarnings("unchecked")
		List<Student> temp = studentDAO.findAll();

		for (int i = 0; i < temp.size(); i++) {
			student.add(temp.get(i));
		}

		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> findTeacher(List<Teacher> teacher) {
		Map<String, String> result = new HashMap<String, String>();
		@SuppressWarnings("unchecked")
		List<Teacher> temp = teacherDAO.findAll();

		for (int i = 0; i < temp.size(); i++) {
			teacher.add(temp.get(i));
		}

		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> modifyStudent (Student student){
		Map<String, String> result = new HashMap<String, String>();
		student.setSid(studentDAO.findBySno(student.getSno()).getSid());
		studentDAO.attachDirty(student);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	public Map<String, String> modifyTeacher (Teacher teacher){
		Map<String, String> result = new HashMap<String, String>();
		teacher.setTid(teacherDAO.findByTno(teacher.getTno()).getTid());
		teacherDAO.attachDirty(teacher);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	public Map<String, String> manageLogin(Manager manager) {
		Map<String, String> result = new HashMap<String, String>();
		Manager managerInDB = (Manager) managerDAO.findByMno(manager.getMno()).get(0);

		if (managerInDB == null) {
			result.put(Constant.ERROR, "Wrong Account Or Password");
			return result;
		} else if (!managerInDB.getPassword().trim()
				.equals(manager.getPassword())) {
			result.put(Constant.ERROR, "Wrong Account Or Password");
			return result;
		}
		HttpSessionUtil.login(managerInDB.getMid(), Constant.MANAGER, null);

		BeanUtils.copyProperties(managerInDB, manager);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> manageLogoff() {
		Map<String, String> result = new HashMap<String, String>();
		HttpSessionUtil.logout();
		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> modifyManagePassword(Manager manager) {
		Map<String, String> result = new HashMap<String, String>();
		Manager m = managerDAO.findById(HttpSessionUtil.getUserId());
		if (m==null) {
			result.put(Constant.ERROR, "Manager Not Exists");
			return result;
		}
		m.setPassword(manager.getPassword());
		managerDAO.attachDirty(m);
		
		result.put(Constant.SUCCESS, null);
		return result;

	}
}
